package com.yjf.ljr.activity;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;

import com.yjf.ljr.R;
import com.yjf.ljr.dataclass.GetNewsInfoDataClass;
import com.yjf.ljr.parser.RequestBuilder.RequestObject;
import com.yjf.ljr.util.AppUtil;

public class NewsDetailActivity extends IjActivity {

	@ID("wvNewsDetail")
	private WebView mWvNewsDetail;
	@ID("tvNewInfoTitle")
	private TextView mTvNewInfoTitle;
	private String detailId;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_news_detail);
		initControls();
	}

	/** 初始化控件 **/
	private void initControls() {
		setLeftBtnClick();
		setTitleStr("新闻公告");

		detailId = getIntent().getStringExtra("newsDetailId");
		if (!TextUtils.isEmpty(detailId)) {
			new GetNewsInfo(mContext).execute();
		}
	}

	class GetNewsInfo extends AsyncTask<Void, Void, String> {
		private GetNewsInfoDataClass dc = new GetNewsInfoDataClass();
		private Context mContext;

		public GetNewsInfo(Context context) {
			this.mContext = context;

		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			RequestObject reqObj = new RequestObject();
			reqObj.method = "getNewsInfo";//
			reqObj.map.put("id", detailId);
			return requestDataAndGetErrorMsg(reqObj, dc);
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);

			if (!TextUtils.isEmpty(result)) {
				showToast(result);
			} else {
				if (!TextUtils.isEmpty(dc.newsInfo.content)) {
					if (!TextUtils.isEmpty(dc.newsInfo.titile)) {
						mTvNewInfoTitle.setText(dc.newsInfo.titile.toString());
					}
					WebSettings settings = mWvNewsDetail.getSettings();
					mWvNewsDetail.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
					settings.setSupportZoom(false);
					settings.setBuiltInZoomControls(false);
					settings.setDomStorageEnabled(true);
					settings.setAppCacheMaxSize(1024 * 1024 * 9);// 设置缓冲大小
					String appCacheDir = mContext.getDir("cache", Context.MODE_PRIVATE).getPath();
					settings.setAppCachePath(appCacheDir);
					settings.setAllowFileAccess(true);
					settings.setAppCacheEnabled(true);
					settings.setCacheMode(WebSettings.LOAD_DEFAULT);
					mWvNewsDetail.loadData(dc.newsInfo.content, "text/html; charset=UTF-8", null);
				} else {
					mWvNewsDetail.setVisibility(View.GONE);
				}
			}

		}
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
		return false;
	}

}
